package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.OrderInfo;
import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.exception.OrderException;
import cn.wolfcode.shop.exception.UserException;
import cn.wolfcode.shop.msg.OrderInfoProducer;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.vo.OrderStatusChangeVo;
import cn.wolfcode.shop.vo.OrderVo;
import cn.wolfcode.util.RedisConstants;
import cn.wolfcode.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.jms.JMSException;
import java.text.MessageFormat;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderInfoProducer orderInfoProducer;
    @Reference
    private IOrderInfoService orderInfoService;

    /**
     * 前台用户生成订单
     * @param token
     * @param orderVo
     * @return
     */
    @PostMapping
    public JSONResultVo generateOrder(@RequestHeader("userToken") String token, @RequestBody OrderVo orderVo) {

        JSONResultVo vo = new JSONResultVo();
        try {
            if (StringUtils.isEmpty(token)) {
                throw new OrderException("参数有误");
            }
            //通过token获取当前登录用户
            String userKey = MessageFormat.format(RedisConstants.USER_TOKEN, token);
            UserLogin userLogin = (UserLogin) redisTemplate.opsForValue().get(userKey);
            if (userLogin == null) {
                vo.setErrorMsg("登录信息已过期,请重新登录");
                return vo;
            }

            orderInfoProducer.sendMsg(userLogin.getId(), orderVo);
            vo.setResult("订单已下达");
        } catch (UserException e) {
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());

        } catch (JMSException ex) {
            ex.printStackTrace();
            vo.setErrorMsg("系统异常,请联系管理员");
        }

        return vo;
    }

    //前台用户确认订单
    @PutMapping()
    public JSONResultVo changeStatus(@RequestHeader("userToken") String token, @RequestBody OrderStatusChangeVo orderStatusChangeVo) {

        JSONResultVo vo = new JSONResultVo();
        try {
            //通过token获取当前登录用户
            String userKey = MessageFormat.format(RedisConstants.USER_TOKEN, token);
            UserLogin userLogin = (UserLogin) redisTemplate.opsForValue().get(userKey);
            if (userLogin == null) {
                vo.setErrorMsg("登录信息已过期,请重新登录");
                return vo;
            }
            //设置用户名
            orderStatusChangeVo.setUserName(userLogin.getUserName());
            //重载该方法
            OrderInfo orderInfo = orderInfoService.changeStatus(orderStatusChangeVo);
            vo.setResult(orderInfo);

        } catch (UserException e) {
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());

        }
        return vo;
    }

}
